﻿using System;
using System.Collections.Specialized;
using System.IO;
using System.Text;
using System.Web;
using System.Web.SessionState;
using WeiXinPF.Modules.CDRT.SmartCity.Application.DTO;
using WeiXinPF.Modules.CDRT.SmartCity.Application.Services;
using WeiXinPF.WeiXinComm;

namespace WeiXinPF.Modules.CDRT.SmartCity.HandlerProcess.Handlers
{
    public class SmartHandler : IHttpHandler, IRequiresSessionState
    {
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        public void ProcessRequest(HttpContext context)
        {
            //获取操作类型
            string action = context.Request.QueryString["action"];
            string json = string.Empty;

            if ("post".Equals(context.Request.HttpMethod.ToLower()))
            {
                StreamReader reader = new StreamReader(context.Request.InputStream);
                json = HttpUtility.UrlDecode(reader.ReadToEnd());
            }
            else
            { 
                json = HttpUtility.UrlDecode(
                    context.Request.QueryString.ToString());
            }

            /*
            JsonSerializer serializer = new JsonSerializer();
            StringReader sr = new StringReader(json);
            object o = serializer.Deserialize(new JsonTextReader(sr), typeof(T));
            */
            switch (action)
            {
                case "save_message":
                    SaveMessage(context);
                    break;
                default:
                    break;
            }
        }

        private void SaveMessage(HttpContext context)
        {
            var userName = context.Request.Form["username"];
            var content = context.Request.Form["content"];
            var mobile = context.Request.Form["tel"];
            var filePath = context.Request.Form["filepath"];
            int wid = Convert.ToInt32(context.Request.Form["wid"]);
            string title = context.Request.Form["title"];
            string idcard = context.Request.Form["idcard"];
            string openid = context.Request.Form["openid"];
            int status = Convert.ToInt32(context.Request.Form["status"]);
            string type = context.Request.Form["type"];

            IMessageService service = new MessageService();
            MessageDTO dto = new MessageDTO();
            dto.user_name = userName;
            dto.content = content;
            dto.mobile = mobile;
            dto.file_path = filePath;
            dto.wid = wid;
            dto.title = title;
            dto.openid = openid;
            dto.idcard = idcard;
            dto.status = status;
            dto.type_ = type;

            service.CreateMessage(dto);
            
            WeiXinResult result = new WeiXinResult();
            string resultJson = Newtonsoft.Json.JsonConvert.SerializeObject(result);
            context.Response.Write(resultJson);
        }
    }
}
